package com.ymdd.library.pickerview.adapter;

import android.content.Context;
import android.text.TextUtils;

import com.ymdd.library.pickerview.utils.PickerConfig;
import com.ymdd.library.pickerview.utils.Utils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by 000098 on 2019/3/26。
 * Description
 *
 * @author 000098
 */
public class WeekNumericWheelAdapter implements WheelAdapter{

    /**
     * The default min value
     */
    public static final int DEFAULT_MAX_VALUE = 9;

    /**
     * The default max value
     */
    private static final int DEFAULT_MIN_VALUE = 0;

    // Values
    public int minValue;
    public int maxValue;
    public List<String> days = new ArrayList<>();
    public List<Date> times = new ArrayList<>();

    // format
    public String format;
    //unit
    public String unit;

    private PickerConfig mPickerConfig;
    /**
     * Constructor
     *
     * @param context the current context
     */
    public WeekNumericWheelAdapter(Context context) {
        this(context, DEFAULT_MIN_VALUE, DEFAULT_MAX_VALUE);
    }

    /**
     * Constructor
     *
     * @param context  the current context
     * @param minValue the wheel min value
     * @param maxValue the wheel max value
     */
    public WeekNumericWheelAdapter(Context context, int minValue, int maxValue) {
        this(context, minValue, maxValue, null);
    }

    /**
     * Constructor
     *
     * @param context  the current context
     * @param minValue the wheel min value
     * @param maxValue the wheel max value
     * @param format   the format string
     */
    public WeekNumericWheelAdapter(Context context, int minValue, int maxValue, String format) {
        this(context, minValue, maxValue, format, null);
    }

    /**
     * Constructor
     *
     * @param context  the current context
     * @param minValue the wheel min value
     * @param maxValue the wheel max value
     * @param format   the format string
     * @param unit     the wheel unit value
     */
    public WeekNumericWheelAdapter(Context context, int minValue, int maxValue, String format, String unit) {
        this.minValue = minValue;
        this.maxValue = maxValue;
        this.format = format;
        this.unit = unit;
    }

    public PickerConfig getConfig() {
        if (mPickerConfig == null)
            mPickerConfig = new PickerConfig();
        return mPickerConfig;
    }

    public void setConfig(PickerConfig config) {
        mPickerConfig = config;
    }

    @Override
    public int getItemsCount() {
        return maxValue - minValue + 1;
    }


    @Override
    public Object getItem(int index) {
        if (index >= 0 && index < getItemsCount()) {
            int value = minValue + index;
            String text = !TextUtils.isEmpty(format) ? String.format(format, value) : Integer.toString(value);

            text = TextUtils.isEmpty(unit) ? text : text + unit;

            return text;
        }
        return null;
    }

    @Override
    public int indexOf(Object o) {
        String text = o.toString();
        //去除时分
        if (text.contains("时") || text.contains("分")) {
            text = Utils.trimSpecialString(text);
        }
        try {
            Integer integer = Integer.valueOf(text);
            return integer - minValue;
        } catch (Exception e) {
            return -1;
        }
    }
}
